package com.yh;

import com.yh.config.AppConfig;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author yuhuan
 * @Date 2022/3/21
 */
@SpringBootApplication
@Import(AppConfig.class)
public class ApiClientApplication implements WebMvcConfigurer {
    public static void main(String[] args) {
        SpringApplication.run(ApiClientApplication.class, args);
    }

    /**
     * 添加跨域映射，开放跨域请求
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/api/**")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                // 允许所有的跨域请求
                .allowedOrigins("*")
                // 允许请求头携带的字段
                .allowedHeaders("token", "orderID", "Content-Type");
    }
}
